package com.mayundev.pa.util;

import com.mayundev.pa.model.ChatRequestEntity;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;

import java.util.ArrayList;
import java.util.List;

public class MessageBuilder {
    public static List<Message> buildMessages(ChatRequestEntity request) {
        List<Message> messages = new ArrayList<>();
        if (request.getHistory() != null) {
            for (var msg : request.getHistory()) {
                switch (msg.getRole()) {
                    case "system":
                        messages.add(new SystemMessage(msg.getContent()));
                        break;
                    case "assistant":
                        StringBuilder sb = new StringBuilder();
                        // 如果存在reasoning，添加思考过程
                        if (msg.getReasoning() != null && !msg.getReasoning().isEmpty()) {
                            sb.append("<think>" + msg.getReasoning() + "</think>");
                            sb.append("\n\n");
                        }
                        sb.append(msg.getContent());
                        messages.add(new AssistantMessage(sb.toString()));
                        break;
                    case "user":
                    default:
                        messages.add(new UserMessage(msg.getContent()));
                        break;
                }
            }
        }
        // 添加当前消息
        messages.add(new UserMessage(request.getContents()));
        return messages;
    }
}
